'use strict';
angular.module('httpWrapService', [])
    .service('httpWrapService', ['$http', '$q', 'dialogs', '$anchorScroll', '$log', '$window','$rootScope', function ($http, $q, dialogs, $anchorScroll, $log, $window, $rootScope) {

        /**
         * get 方式提交数据
         * @param url
         * @param params
         * @returns {r.promise|promise|AnimateRunner.promise|qFactory.Deferred.promise|x.ready.promise}
         */
        this.get = function (url, params) {
            var deferred = $q.defer();
            var target = this;
            $http({
                method: 'GET',
                url: url,
                data: params
            }).then(function (res) {
                if (res.status == '200' && res.data.success) {
                    deferred.resolve(res);
                } else if (res.status == '200' && !res.data.success) {
                    deferred.reject(res);
                }
            }, function (res) {
                $log.error(res);
                res.data = {};
                res.data.success = false;
                if (res.status) {
                    res.status = res.status;
                    res.data.msg = '服务器错误';
                } else {
                    res.status = '100';
                    res.data.msg = '客户端错误';
                }
                deferred.reject(res);
            });
            return deferred.promise;
        }

        /**
         * post提交数据
         * @param url
         * @param params
         * @returns {r.promise|promise|AnimateRunner.promise|qFactory.Deferred.promise|x.ready.promise}
         */
        this.post = function (url, params) {
            var deferred = $q.defer();
            var target = this;
            $http({
                method: 'POST',
                url: url,
                data: params
            }).then(function (res) {
                if (res.status == '200' && res.data.success) {
                    deferred.resolve(res);
                } else if (res.status == '200' && !res.data.success) {
                    deferred.reject(res);
                }
            }, function (res) {
                $log.error(res);
                res.data = {};
                res.data.success = false;
                if (res.status) {
                    res.status = res.status;
                    res.data.msg = '服务器错误';
                } else {
                    res.status = '100';
                    res.data.msg = '客户端错误';
                }
                deferred.reject(res);
            });
            return deferred.promise;
        }


        /**
         * 同步请求获取数据
         * post提交数据
         * @param url
         * @param params
         * @param callback   请求成功后回调函数
         */
        this.asyncPost = function (url, params, callback) {
            var target = this;
            $.ajax({
                type: "POST",
                url: url,
                async: false,
                contentType: 'application/json;charset=UTF-8',
                dataType: "json",
                data: angular.toJson(params),
                statusCode: {
                    403: target.login
                },
                success: function (data) {
                    callback(data);
                }
            });
        }


        /**
         * 提供给一个确认框提交数据
         * @param url
         * @param params
         * @param confirmMsg
         * @returns {r.promise|promise|AnimateRunner.promise|qFactory.Deferred.promise|x.ready.promise}
         */
        this.confirmPost = function (url, params, confirmMsg) {
            var deferred = $q.defer();
            var cmsg = '确认删除数据？';
            if (confirmMsg) {
                cmsg = confirmMsg;
            }
            var dlg = dialogs.confirm('确认', cmsg, {size: 'sm'});
            dlg.result.then(function (btn) {
                $http({
                    method: 'POST',
                    url: url,
                    data: params
                }).then(function (res) {
                    if (res.status == '200' && res.data.success) {
                        deferred.resolve(res);
                    } else if (res.status == '200' && !res.data.success) {
                        deferred.reject(res);
                    }
                }, function (res) {
                    $log.error(res);
                    res.data = {};
                    res.data.success = false;
                    if (res.status) {
                        res.status = res.status;
                        res.data.msg = '服务器错误';
                    } else {
                        res.status = '100';
                        res.data.msg = '客户端错误';
                    }
                    deferred.reject(res);
                });

            }, function (btn) {
            });
            return deferred.promise;
        }


        /**
         * 默认的删除方法
         * @param url
         * @param deleteParams
         * @param confirmMsg
         * @returns {r.promise|promise|AnimateRunner.promise|qFactory.Deferred.promise|x.ready.promise}
         */
        this.defaultDelete = function (url, deleteParams, confirmMsg) {
            var deferred = $q.defer();
            this.confirmPost(url, deleteParams, confirmMsg).then(function (res) {
                deferred.resolve(res);
            }, function (res) {
                dialogs.notify('错误', res.data.msg, {size: 'sm'});
                deferred.reject(res);
            });
            return deferred.promise;
        }
        
        /**
         * 默认的删除方法
         * @param url
         * @param deleteParams
         * @param confirmMsg
         * @returns {r.promise|promise|AnimateRunner.promise|qFactory.Deferred.promise|x.ready.promise}
         */
        this.defaultFootTableDelete = function (Table, id, url, deleteParams, confirmMsg) {
        	var service = this;
        	if(!Table.deleteDisable) Table.deleteDisable = {};
        	if(Table.deleteDisable[id]) {
        		 dialogs.notify('错误', '请求已经提交，请不要重复提交！', {size: 'sm'});
        		return ;
        	}
        	Table.deleteDisable[id] = id;
            
            var deferred = $q.defer();
            var cmsg = '确认删除数据？';
            if (confirmMsg) {
                cmsg = confirmMsg;
            }
            var dlg = dialogs.confirm('确认', cmsg, {size: 'sm'});
            dlg.result.then(function (btn) {
            	service.post(url, deleteParams).then(function(res){
            		deferred.resolve(res);
                	Table.deleteDisable[id] = undefined;
            	},function(res){
            		dialogs.notify('错误', res.data.msg, {size: 'sm'});
                    Table.deleteDisable[id] = undefined;
                    deferred.reject(res);
            	});
            }, function (btn) {
            	Table.deleteDisable[id] = undefined;
            });
            return deferred.promise;
        }

        /**
         * 默认的删除方法
         * @param url
         * @param deleteParams
         * @param confirmMsg
         * @returns {r.promise|promise|AnimateRunner.promise|qFactory.Deferred.promise|x.ready.promise}
         */
        this.defaultGridDelete = function (grid, id, url, deleteParams, confirmMsg) {
            grid.deletes[id] = id;
            var deferred = $q.defer();
            this.confirmPost(url, deleteParams, confirmMsg).then(function (res) {
                deferred.resolve(res);
                grid.deletes[id] = undefined;
            }, function (res) {
                dialogs.notify('错误', res.data.msg, {size: 'sm'});
                grid.deletes[id] = undefined;
                deferred.reject(res);
            });
            return deferred.promise;
        }

        /**
         * 默认保存方法
         * @param url
         * @param params
         * @returns {r.promise|promise|AnimateRunner.promise|qFactory.Deferred.promise|x.ready.promise}
         */
        this.defaultSave = function (url, params, scope) {
            scope.saveBtnDisable = true;
            var deferred = $q.defer();
            this.post(url, params).then(function (res) {
                scope.saveBtnDisable = false;
                deferred.resolve(res);
            }, function (res) {
                scope.Alert.addAlert(res.data.msg);
                //$location.hash($scope.scroll);
                $anchorScroll();
                scope.saveBtnDisable = false;
                deferred.reject(res);
            });
            return deferred.promise;
        }

        this.defaultPost = function (url, params) {
            var deferred = $q.defer();
            this.post(url, params).then(function (res) {
                deferred.resolve(res);
            }, function (res) {
                dialogs.notify('错误', res.data.msg, {size: 'sm'});
                deferred.reject(res);
            });
            return deferred.promise;
        }


    }]);